package com.finace.money.ui.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.EmptyViewUtil;
import com.finace.money.common.util.TabLayoutUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.AnalyseListBean;
import com.finace.money.data.bean.DealRecordListBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.di.component.DaggerMainNewsComponent;
import com.finace.money.di.module.DealRecordModule;
import com.finace.money.di.module.MainNewsModule;
import com.finace.money.interfaces.LoadMoreOnScrollListener;
import com.finace.money.interfaces.OnItemClickListener;
import com.finace.money.presenter.DealRecordPresenter;
import com.finace.money.presenter.MainNewsPresenter;
import com.finace.money.presenter.contract.DealRecordContract;
import com.finace.money.presenter.contract.MainNewsContract;
import com.finace.money.ui.activity.WebViewActivity;
import com.finace.money.ui.adapter.HomeNewsAdapter;
import com.finace.money.ui.adapter.HomeRecordAdapter;
import com.finace.money.ui.adapter.MainViewPagerAdapter;
import com.finace.money.ui.adapter.NewsAnalyseAdapter;
import com.finace.money.ui.adapter.holder.FootViewHolder;
import com.finace.money.ui.widget.CusRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2017/11/23.
 */

public class MainNewsFragment extends BaseFragment<MainNewsPresenter> implements DealRecordContract.View,
        MainNewsContract.View {
    @BindView(R.id.tab_news)
    TabLayout tabNews;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.swipeRefreshLayout)
    CusRefreshLayout swipeRefreshLayout;

    @Inject
    DealRecordPresenter mDealRecordPresenter;
    private HomeRecordAdapter homeRecordAdapter;
    private NewsAnalyseAdapter newsAnalyseAdapter;
    private HomeNewsAdapter homeNewsAdapter;

    private int pageNumber_deal = 1;//交易攻略
    private int pageNumber_analyse = 1;//行情分析
    private int pageNumber_news = 1;//行业资讯

    List<DealRecordListBean.RowsBean> mDataDeal = new ArrayList<>();
    List<AnalyseListBean.RowsBean> mDataAnalyse = new ArrayList<>();
    List<DealRecordListBean.RowsBean> mDataNews = new ArrayList<>();
    private boolean isLoading = true;
    private int mPagePosition = 0;


    private EmptyViewUtil emptyViewUtil;

    @Override
    public int setLayout() {
        return R.layout.fragment_news;
    }

    @Override
    public void init() {
        emptyViewUtil  = new EmptyViewUtil(getActivity()).emptyViewMsg(R.string.content_empty);
        mDealRecordPresenter.getList(Constant.page_deal_record, "10", String.valueOf(pageNumber_deal));

        initViewPager();
        tabNews.setupWithViewPager(viewPager);
        TabLayoutUtil.reflex(tabNews);
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorYellowDDC09C);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initList();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mPagePosition = position;
                initList();
            }
        });
        homeRecordAdapter.setmListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                WebViewActivity.launcher(UIUtil.getContext(),mDataDeal.get(position).getLink(),"交易攻略");
            }
        });
        newsAnalyseAdapter.setmListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                WebViewActivity.launcher(UIUtil.getContext(),mDataAnalyse.get(position).getUrl(),"行情分析");
            }
        });
        homeNewsAdapter.setmListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                WebViewActivity.launcher(UIUtil.getContext(),mDataNews.get(position).getLink(),"行业资讯");
            }
        });
    }

    /**
     * 加载列表
     */
    private void initList() {
        switch (mPagePosition) {
            case 0:
                pageNumber_deal = 1;
                mDealRecordPresenter.getList(Constant.page_deal_record, "10", String.valueOf(pageNumber_deal));
                break;
            case 1:
                pageNumber_analyse = 1;
                mPresenter.getAnalyseList("10", String.valueOf(pageNumber_analyse));
                break;
            case 2:
                pageNumber_news = 1;
                mDealRecordPresenter.getList(Constant.page_news, "10", String.valueOf(pageNumber_news));
                break;
        }
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

        DaggerMainNewsComponent.builder().appComponent(appComponent)
                .mainNewsModule(new MainNewsModule(this))
                .dealRecordModule(new DealRecordModule(this))
                .build().inject(this);
    }

    private void initViewPager() {
        ArrayList<View> views = new ArrayList<>();
        ArrayList<String> titles = new ArrayList<>();
        //交易攻略
        View viewRecord = LayoutInflater.from(UIUtil.getContext()).inflate(R.layout.recycle_view, viewPager, false);
        RecyclerView recyclerViewRecord = viewRecord.findViewById(R.id.recyclerView_home);
        LinearLayoutManager recordManager = new LinearLayoutManager(UIUtil.getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerViewRecord.setLayoutManager(recordManager);
        homeRecordAdapter = new HomeRecordAdapter();
        recyclerViewRecord.setAdapter(homeRecordAdapter);
        views.add(viewRecord);
        titles.add(getString(R.string.trading_strategy));
        loadMore(recyclerViewRecord, recordManager);

        //行情分析
        View viewAnalyse = LayoutInflater.from(UIUtil.getContext()).inflate(R.layout.recycle_view, viewPager, false);
        RecyclerView recyclerViewAnalyse = viewAnalyse.findViewById(R.id.recyclerView_home);
        recyclerViewAnalyse.setPadding(0, UIUtil.dip2px(20), 0, 0);
        recyclerViewAnalyse.setBackgroundColor(UIUtil.getColor(R.color.colorBlack15191F));
        LinearLayoutManager analyseLayoutManager = new LinearLayoutManager(UIUtil.getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerViewAnalyse.setLayoutManager(analyseLayoutManager);
        newsAnalyseAdapter = new NewsAnalyseAdapter();
        recyclerViewAnalyse.setAdapter(newsAnalyseAdapter);
        views.add(recyclerViewAnalyse);
        titles.add(getString(R.string.quotations_analysis));
        loadMore(recyclerViewAnalyse, analyseLayoutManager);

        //行业资讯
        View viewNews = LayoutInflater.from(UIUtil.getContext()).inflate(R.layout.recycle_view, viewPager, false);
        RecyclerView recyclerViewNews = viewNews.findViewById(R.id.recyclerView_home);
        LinearLayoutManager newsLayoutManager = new LinearLayoutManager(UIUtil.getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerViewNews.setLayoutManager(newsLayoutManager);
        homeNewsAdapter = new HomeNewsAdapter();
        recyclerViewNews.setAdapter(homeNewsAdapter);
        views.add(viewNews);
        titles.add(getString(R.string.industry_information));
        loadMore(recyclerViewNews, newsLayoutManager);

        MainViewPagerAdapter mainViewPagerAdapter = new MainViewPagerAdapter(views, titles);
        viewPager.setAdapter(mainViewPagerAdapter);
    }

    private void loadMore(RecyclerView recyclerView, LinearLayoutManager recordManager) {

        recyclerView.addOnScrollListener(new LoadMoreOnScrollListener(recordManager) {
            @Override
            public void onLoadMore() {

                if (isLoading) {
                    switch (mPagePosition) {
                        case 0:
                            pageNumber_deal++;
                            homeRecordAdapter.changeState(1);
                            mDealRecordPresenter.getList(Constant.page_deal_record, "10", String.valueOf(pageNumber_deal));
                            break;
                        case 1:
                            pageNumber_analyse++;
                            homeNewsAdapter.changeState(1);
                            mPresenter.getAnalyseList("10", String.valueOf(pageNumber_analyse));
                            break;
                        case 2:
                            pageNumber_news++;
                            homeNewsAdapter.changeState(1);
                            mDealRecordPresenter.getList(Constant.page_news, "10", String.valueOf(pageNumber_news));
                            break;
                    }
                } else {
                    switch (mPagePosition) {
                        case 0:
                            pageNumber_deal++;
                            homeRecordAdapter.changeState(2);
                            break;
                        case 1:
                            pageNumber_analyse++;
                            newsAnalyseAdapter.changeState(2);
                            break;
                        case 2:
                            pageNumber_news++;
                            homeNewsAdapter.changeState(2);
                            break;
                    }
                    UIUtil.showToastSafe(getString(R.string.date_have_null_tip));
                    isLoading = false;
                }

            }
        });
    }

    @Override
    public void getDealRecordListSucess(int type, DealRecordListBean dealRecordListBean) {
        if (null != dealRecordListBean) {
            List<DealRecordListBean.RowsBean> rows = dealRecordListBean.getRows();
            if (null != rows && rows.size() > 0) {
                switch (mPagePosition) {
                    case 0:
                        if (pageNumber_deal == 1) {
                            isLoading = true;
                            mDataDeal.clear();
                        }
                        homeRecordAdapter.changeState(0);
                        mDataDeal.addAll(rows);
                        homeRecordAdapter.setmDatas(mDataDeal);
                        break;
                    case 2:
                        if (pageNumber_news == 1) {
                            isLoading = true;
                            mDataNews.clear();
                        }
                        homeNewsAdapter.changeState(0);
                        mDataNews.addAll(rows);
                        homeNewsAdapter.setmDatas(mDataNews);
                        break;
                }

            } else {
                isLoading = false;
                switch (mPagePosition) {
                    case 0:
                        if (pageNumber_deal != 1) {
                            UIUtil.showToastSafe(getString(R.string.date_have_null_tip));
                        }
                        homeRecordAdapter.changeState(2);
                        break;
                    case 2:

                        if (pageNumber_news != 1) {
                            UIUtil.showToastSafe(getString(R.string.date_have_null_tip));
                        }
                        homeNewsAdapter.changeState(2);

                        break;
                }

            }


            if (mDataDeal.size() > 0) {
                emptyViewUtil.emptyViewHiden();
            } else {
                emptyViewUtil.emptyViewShow();
                homeRecordAdapter.changeState(FootViewHolder.EMPTY);
            }

            if (mDataNews.size() > 0) {
                emptyViewUtil.emptyViewHiden();
            } else {
                emptyViewUtil.emptyViewShow();
                homeNewsAdapter.changeState(FootViewHolder.EMPTY);
            }

            switch (mPagePosition) {
                case 0:
                    homeRecordAdapter.notifyDataSetChanged();
                    break;
                case 2:
                    homeNewsAdapter.notifyDataSetChanged();
                    break;
            }
        }
    }

    @Override
    public void getAnalyseListSucess(AnalyseListBean analyseListBean) {
        if (null != analyseListBean) {
            List<AnalyseListBean.RowsBean> rows = analyseListBean.getRows();

            if (null != rows && rows.size() > 0) {
                if (pageNumber_analyse == 1) {
                    isLoading = true;
                    mDataAnalyse.clear();
                }
                newsAnalyseAdapter.changeState(0);
                mDataAnalyse.addAll(rows);
                newsAnalyseAdapter.setmDatas(mDataAnalyse);
            } else {
                isLoading = false;
                if (pageNumber_analyse != 1) {
                    UIUtil.showToastSafe(getString(R.string.date_have_null_tip));
                }
                newsAnalyseAdapter.changeState(2);
            }
            if (mDataAnalyse.size() > 0) {
                emptyViewUtil.emptyViewHiden();
            } else {
                emptyViewUtil.emptyViewShow();
                newsAnalyseAdapter.changeState(FootViewHolder.EMPTY);
            }
            newsAnalyseAdapter.notifyDataSetChanged();
        }
    }
}
